import axios from 'axios'
import Vue from 'vue'
import Vuex from 'vuex'

import "../api/index"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // shoplist 的表头数据
    tableList: [
      {
        prop: "title",
        label: "店铺名称",
      },
      {
        prop: "city",
        label: "店铺地址",
      },
      {
        prop: "tal",
        label: "店铺电话",
      },
    ],
    // shoplist 的渲染数据
    tableData: []
  },
  mutations: {
    // shoplist 获取数据
    gettableData(state, payload) {
      state.tableData = payload
    },
    // shoplist编辑
    shoplistEdit(state, payload) {
      state.tableData.map((item, index) => {
        if (item.id == payload.id) {
          item.title = payload.title;
          item.tal = payload.tal;
          item.city = payload.city;
          console.log(1);
        }
        return item
      })
    },
    //新增数据
    shoplistAdd(state, payload) {
      const { id, title, tal, city } = payload
      state.tableData.unshift({ id, title, tal, city })
      console.log(state.tableData);
    },
    // 删除
    shoplistDel(state, payload) {
      state.tableData.forEach((item, index) => {
        if(item.id==payload){
          state.tableData.splice(index, 1)
        }
      })
    }
  },
  actions: {
    // shoplist 获取数据
    asyncgettableData(context) {
      axios.get('/api/list').then(res => {
        // console.log(res.data);
        context.commit('gettableData', res.data.data)
      })
    }
  },
  modules: {
  }

})

